﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace CsPotrace
{
    public class Filterset
    {
        private static int[] gaussMatrix = {
                                             2,  4,  5,  4, 2,
                                             4,  9, 12,  9, 4,
                                             5, 12, 15, 12, 5,
                                             4,  9, 12,  9, 4,
                                             2,  4,  5,  4, 2
                                           };
        public static Bitmap rgbMapGaussian(Bitmap me)
        {
            int width = me.Width;
            int height = me.Height;
            int firstX = 2;
            int lastX = width - 3;
            int firstY = 2;
            int lastY = height - 3;

            Bitmap newGm = new Bitmap(width, height);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    /* image boundaries */
                    if (x < firstX || x > lastX || y < firstY || y > lastY)
                    {

                        newGm.SetPixel(x, y, me.GetPixel(x, y));
                        continue;
                    }

                    /* all other pixels */
                    int gaussIndex = 0;
                    int sumR = 0;
                    int sumG = 0;
                    int sumB = 0;
                    for (int i = y - 2; i <= y + 2; i++)
                    {
                        for (int j = x - 2; j <= x + 2; j++)
                        {
                            int weight = gaussMatrix[gaussIndex++];
                            Color rgb = me.GetPixel(j, i);
                            sumR += weight * (int)rgb.R;
                            sumG += weight * (int)rgb.G;
                            sumB += weight * (int)rgb.B;
                        }
                    }

                    int R = (sumR / 159) & 0xff;
                    int G = (sumG / 159) & 0xff;
                    int B = (sumB / 159) & 0xff;
                    Color rout = Color.FromArgb(R, G, B);
                    newGm.SetPixel(x, y, rout);
                }
            }

            return newGm;

        }
    }
}
